
package dynamicdiscountsystem;

import com.mysql.jdbc.PreparedStatement;
import java.awt.Color;
import java.io.FileNotFoundException;
import java.io.FileReader;

import java.io.IOException;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.Properties;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JOptionPane;
import javax.swing.table.DefaultTableCellRenderer;
import javax.swing.table.DefaultTableModel;
import javax.swing.table.TableColumn;
import javax.swing.table.TableModel;

/**
 *
 * @author atilla1991
 */
public class SellingFrame extends javax.swing.JInternalFrame {

    /** Creates new form SellingFrame */
    public SellingFrame() {
        initComponents();
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jPanel1 = new javax.swing.JPanel();
        jButtonRight = new javax.swing.JButton();
        jButtonLeft = new javax.swing.JButton();
        jPanel4 = new javax.swing.JPanel();
        jScrollPane1 = new javax.swing.JScrollPane();
        jTable1 = new javax.swing.JTable();
        jPanel5 = new javax.swing.JPanel();
        jScrollPane2 = new javax.swing.JScrollPane();
        jTable2 = new javax.swing.JTable();
        jPanel2 = new javax.swing.JPanel();
        jSellingButton = new javax.swing.JButton();
        jSaleCancelButton = new javax.swing.JButton();
        jPriceCountButton = new javax.swing.JButton();
        jLabel1 = new javax.swing.JLabel();
        jLabelTotalPrice = new javax.swing.JLabel();

        setTitle("Mal Satışı");
        setAutoscrolls(true);
        addInternalFrameListener(new javax.swing.event.InternalFrameListener() {
            public void internalFrameActivated(javax.swing.event.InternalFrameEvent evt) {
                formİnternalFrameActivated(evt);
            }
            public void internalFrameClosed(javax.swing.event.InternalFrameEvent evt) {
            }
            public void internalFrameClosing(javax.swing.event.InternalFrameEvent evt) {
            }
            public void internalFrameDeactivated(javax.swing.event.InternalFrameEvent evt) {
            }
            public void internalFrameDeiconified(javax.swing.event.InternalFrameEvent evt) {
            }
            public void internalFrameIconified(javax.swing.event.InternalFrameEvent evt) {
            }
            public void internalFrameOpened(javax.swing.event.InternalFrameEvent evt) {
            }
        });
        getContentPane().setLayout(new org.netbeans.lib.awtextra.AbsoluteLayout());

        jPanel1.setBorder(javax.swing.BorderFactory.createEtchedBorder());

        jButtonRight.setIcon(new javax.swing.ImageIcon(getClass().getResource("/dynamicdiscountsystem/resources/right.png"))); // NOI18N
        jButtonRight.setBorder(javax.swing.BorderFactory.createEmptyBorder(1, 1, 1, 1));
        jButtonRight.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButtonRightActionPerformed(evt);
            }
        });

        jButtonLeft.setIcon(new javax.swing.ImageIcon(getClass().getResource("/dynamicdiscountsystem/resources/left.png"))); // NOI18N
        jButtonLeft.setBorder(javax.swing.BorderFactory.createEmptyBorder(1, 1, 1, 1));
        jButtonLeft.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButtonLeftActionPerformed(evt);
            }
        });

        jPanel4.setBorder(javax.swing.BorderFactory.createTitledBorder(null, "Ümumi Malların Siyahısı", javax.swing.border.TitledBorder.DEFAULT_JUSTIFICATION, javax.swing.border.TitledBorder.DEFAULT_POSITION, new java.awt.Font("Tahoma", 0, 12))); // NOI18N

        jTable1.setAutoCreateRowSorter(true);
        jTable1.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {

            },
            new String [] {
                "ID", "Malın Adı", "Malın Növü", "Malın Sayı", "Malın Alış Qiyməti", "Malın Satış Qiyməti"
            }
        ) {
            Class[] types = new Class [] {
                java.lang.Long.class, java.lang.String.class, java.lang.String.class, java.lang.Integer.class, java.lang.Double.class, java.lang.Double.class
            };
            boolean[] canEdit = new boolean [] {
                false, false, false, false, false, false
            };

            public Class getColumnClass(int columnIndex) {
                return types [columnIndex];
            }

            public boolean isCellEditable(int rowIndex, int columnIndex) {
                return canEdit [columnIndex];
            }
        });
        jScrollPane1.setViewportView(jTable1);

        javax.swing.GroupLayout jPanel4Layout = new javax.swing.GroupLayout(jPanel4);
        jPanel4.setLayout(jPanel4Layout);
        jPanel4Layout.setHorizontalGroup(
            jPanel4Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel4Layout.createSequentialGroup()
                .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 577, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );
        jPanel4Layout.setVerticalGroup(
            jPanel4Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel4Layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        jPanel5.setBorder(javax.swing.BorderFactory.createTitledBorder(null, "Endirimli Malların Siyahısı", javax.swing.border.TitledBorder.DEFAULT_JUSTIFICATION, javax.swing.border.TitledBorder.DEFAULT_POSITION, new java.awt.Font("Tahoma", 0, 12))); // NOI18N

        jTable2.setAutoCreateRowSorter(true);
        jTable2.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {

            },
            new String [] {
                "ID", "Malın Adı", "Malın Növü", "Malın Sayı", "Malın Alış Qiyməti", "Malın Satış Qiyməti", "Alınan Malın Sayı"
            }
        ) {
            Class[] types = new Class [] {
                java.lang.Long.class, java.lang.String.class, java.lang.String.class, java.lang.Integer.class, java.lang.Double.class, java.lang.Double.class, java.lang.Integer.class
            };
            boolean[] canEdit = new boolean [] {
                false, false, false, false, false, false, true
            };

            public Class getColumnClass(int columnIndex) {
                return types [columnIndex];
            }

            public boolean isCellEditable(int rowIndex, int columnIndex) {
                return canEdit [columnIndex];
            }
        });
        jScrollPane2.setViewportView(jTable2);

        javax.swing.GroupLayout jPanel5Layout = new javax.swing.GroupLayout(jPanel5);
        jPanel5.setLayout(jPanel5Layout);
        jPanel5Layout.setHorizontalGroup(
            jPanel5Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel5Layout.createSequentialGroup()
                .addComponent(jScrollPane2, javax.swing.GroupLayout.PREFERRED_SIZE, 577, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );
        jPanel5Layout.setVerticalGroup(
            jPanel5Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel5Layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jScrollPane2, javax.swing.GroupLayout.DEFAULT_SIZE, 423, Short.MAX_VALUE)
                .addContainerGap())
        );

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jPanel4, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jButtonRight, javax.swing.GroupLayout.PREFERRED_SIZE, 47, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jButtonLeft, javax.swing.GroupLayout.PREFERRED_SIZE, 47, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jPanel5, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addContainerGap())
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addGap(100, 100, 100)
                        .addComponent(jButtonRight)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addComponent(jButtonLeft))
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addContainerGap()
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                            .addComponent(jPanel5, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                            .addComponent(jPanel4, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))))
                .addContainerGap())
        );

        getContentPane().add(jPanel1, new org.netbeans.lib.awtextra.AbsoluteConstraints(10, 133, 1270, -1));

        jPanel2.setBorder(javax.swing.BorderFactory.createTitledBorder(null, "Əməliyyatlar", javax.swing.border.TitledBorder.DEFAULT_JUSTIFICATION, javax.swing.border.TitledBorder.DEFAULT_POSITION, new java.awt.Font("Tahoma", 0, 12))); // NOI18N

        jSellingButton.setFont(new java.awt.Font("Tahoma", 0, 14)); // NOI18N
        jSellingButton.setIcon(new javax.swing.ImageIcon(getClass().getResource("/dynamicdiscountsystem/resources/corbeille_box_sale_v.png"))); // NOI18N
        jSellingButton.setText("Sat");
        jSellingButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jSellingButtonActionPerformed(evt);
            }
        });

        jSaleCancelButton.setFont(new java.awt.Font("Tahoma", 0, 14));
        jSaleCancelButton.setIcon(new javax.swing.ImageIcon(getClass().getResource("/dynamicdiscountsystem/resources/Cancel.png"))); // NOI18N
        jSaleCancelButton.setText("Satışı Rədd Et");
        jSaleCancelButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jSaleCancelButtonActionPerformed(evt);
            }
        });

        jPriceCountButton.setFont(new java.awt.Font("Tahoma", 0, 14)); // NOI18N
        jPriceCountButton.setIcon(new javax.swing.ImageIcon(getClass().getResource("/dynamicdiscountsystem/resources/calculator.png"))); // NOI18N
        jPriceCountButton.setText("Məbləği Hesabla");
        jPriceCountButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jPriceCountButtonActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout jPanel2Layout = new javax.swing.GroupLayout(jPanel2);
        jPanel2.setLayout(jPanel2Layout);
        jPanel2Layout.setHorizontalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel2Layout.createSequentialGroup()
                .addGap(23, 23, 23)
                .addComponent(jSellingButton)
                .addGap(18, 18, 18)
                .addComponent(jPriceCountButton, javax.swing.GroupLayout.PREFERRED_SIZE, 159, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(18, 18, 18)
                .addComponent(jSaleCancelButton)
                .addContainerGap(43, Short.MAX_VALUE))
        );
        jPanel2Layout.setVerticalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel2Layout.createSequentialGroup()
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jSellingButton, javax.swing.GroupLayout.PREFERRED_SIZE, 31, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jPriceCountButton, javax.swing.GroupLayout.PREFERRED_SIZE, 31, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jSaleCancelButton, javax.swing.GroupLayout.PREFERRED_SIZE, 31, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        getContentPane().add(jPanel2, new org.netbeans.lib.awtextra.AbsoluteConstraints(10, 11, -1, -1));

        jLabel1.setFont(new java.awt.Font("Tahoma", 0, 24));
        jLabel1.setText("Məbləğ:  ");
        getContentPane().add(jLabel1, new org.netbeans.lib.awtextra.AbsoluteConstraints(516, 29, 104, -1));

        jLabelTotalPrice.setFont(new java.awt.Font("Tahoma", 0, 24)); // NOI18N
        getContentPane().add(jLabelTotalPrice, new org.netbeans.lib.awtextra.AbsoluteConstraints(638, 29, 267, 29));

        pack();
    }// </editor-fold>//GEN-END:initComponents
    double[] productPrices;

    public void updateFrame() throws SQLException {
        jLabelTotalPrice.setText("");
        DatabaseConnection connect = new DatabaseConnection();

        try {
            DefaultTableModel model1 = (DefaultTableModel) jTable1.getModel();
            DefaultTableModel model2 = (DefaultTableModel) jTable2.getModel();
            while (model1.getRowCount() > 0) {
                model1.removeRow(0);
            }
            while (model2.getRowCount() > 0) {
                model2.removeRow(0);
            }

            String command = "SELECT * FROM PRODUCT_INPUT ORDER BY PRODUCT_NAME,PRODUCT_MODEL ASC";
            ResultSet rs = connect.ExecuteQuery(command);
            while (rs.next()) {

                Integer idPRODUCT_INPUT = rs.getInt("idPRODUCT_INPUT");
                String PRODUCT_NAME = rs.getString("PRODUCT_NAME");
                String PRODUCT_MODEL = rs.getString("PRODUCT_MODEL");
                Integer PRODUCT_AMOUNT = rs.getInt("PRODUCT_AMOUNT");
                Double PRODUCT_PURCHASE_COST = rs.getDouble("PRODUCT_PURCHASE_COST");
                Double PRODUCT_SALE_COST = rs.getDouble("PRODUCT_SALE_COST");
                Object[] newRow = new Object[]{idPRODUCT_INPUT, PRODUCT_NAME, PRODUCT_MODEL, PRODUCT_AMOUNT, PRODUCT_PURCHASE_COST, PRODUCT_SALE_COST};
                model1.addRow(newRow);
            }
        } catch (IOException ex) {
            JOptionPane.showMessageDialog(null,
                    "Xəta baş verdi! \nXəta mesajı = " + ex.getMessage(),
                    "XƏTA", JOptionPane.ERROR_MESSAGE);
        }
    }

    private void jButtonRightActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButtonRightActionPerformed

        int rowCount = jTable1.getSelectedRowCount();
        DefaultTableModel model1 = (DefaultTableModel) jTable1.getModel();
        DefaultTableModel model2 = (DefaultTableModel) jTable2.getModel();

        if (rowCount > 0) {
            int[] selectedRows = jTable1.getSelectedRows();
            int selectedRow = 0;
            int count = 0;
            for (int i = 0; i < rowCount; i++) {

                selectedRow = selectedRows[i] - count;
                //JOptionPane.showMessageDialog(null, model1.getValueAt(selectedRow, 0)+"   "+selectedRow);
                Integer idPRODUCT_INPUT = (Integer) model1.getValueAt(selectedRow, 0);
                String PRODUCT_NAME = (String) model1.getValueAt(selectedRow, 1);
                String PRODUCT_MODEL = (String) model1.getValueAt(selectedRow, 2);
                Integer PRODUCT_AMOUNT = (Integer) model1.getValueAt(selectedRow, 3);
                Double PRODUCT_PURCHASE_COST = (Double) model1.getValueAt(selectedRow, 4);
                Double PRODUCT_SALE_COST = (Double) model1.getValueAt(selectedRow, 5);
                Object[] newRow = new Object[]{idPRODUCT_INPUT, PRODUCT_NAME, PRODUCT_MODEL, PRODUCT_AMOUNT, PRODUCT_PURCHASE_COST, PRODUCT_SALE_COST};
                model2.addRow(newRow);
                model1.removeRow(selectedRow);
                count++;
            }

        }
    }//GEN-LAST:event_jButtonRightActionPerformed

    private void jButtonLeftActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButtonLeftActionPerformed

        int rowCount = jTable2.getSelectedRowCount();
        DefaultTableModel model1 = (DefaultTableModel) jTable1.getModel();
        DefaultTableModel model2 = (DefaultTableModel) jTable2.getModel();

        if (rowCount > 0) {
            int[] selectedRows = jTable2.getSelectedRows();
            int selectedRow = 0;
            int count = 0;
            for (int i = 0; i < rowCount; i++) {

                selectedRow = selectedRows[i] - count;
                //JOptionPane.showMessageDialog(null, model1.getValueAt(selectedRow, 0)+"   "+selectedRow);
                Integer idPRODUCT_INPUT = (Integer) model2.getValueAt(selectedRow, 0);
                String PRODUCT_NAME = (String) model2.getValueAt(selectedRow, 1);
                String PRODUCT_MODEL = (String) model2.getValueAt(selectedRow, 2);
                Integer PRODUCT_AMOUNT = (Integer) model2.getValueAt(selectedRow, 3);
                Double PRODUCT_PURCHASE_COST = (Double) model2.getValueAt(selectedRow, 4);
                Double PRODUCT_SALE_COST = (Double) model2.getValueAt(selectedRow, 5);
                Object[] newRow = new Object[]{idPRODUCT_INPUT, PRODUCT_NAME, PRODUCT_MODEL, PRODUCT_AMOUNT, PRODUCT_PURCHASE_COST, PRODUCT_SALE_COST};
                model1.addRow(newRow);
                model2.removeRow(selectedRow);
                count++;
            }

        }
}//GEN-LAST:event_jButtonLeftActionPerformed

    public void checkDiscounts() {
        java.sql.Date dateFrom;
        java.sql.Date dateTo;
        java.sql.Date currentDate = new java.sql.Date(new Date().getTime());

        try {
            DatabaseConnection con = new DatabaseConnection();
            //1-ci endirim-in DataBase - ni yoxluyuruq eger endirimin vaxtı keçibsə
            //bu endirimin tətbiq olunduğu mallar Endirimli malların
            //sırasından çıxarılır.
            ResultSet rs = con.ExecuteQuery("SELECT DATE_FROM,DATE_TO FROM DISCOUNT1 "
                    + "ORDER BY idDISCOUNT1 DESC LIMIT 1");
            rs.next();
            dateFrom = rs.getDate("DATE_FROM");
            dateTo = rs.getDate("DATE_TO");
            if (currentDate.compareTo(dateTo) > 0) {
                con.Execute("DELETE FROM DISCOUNT_APPLICATION WHERE DISCOUNT_NUMBER = 1");
            }

            //2-ci endirim-in DataBase - ni yoxluyuruq eger endirimin vaxtı keçibsə
            //bu endirimin tətbiq olunduğu mallar Endirimli malların
            //sırasından çıxarılır.
            rs = con.ExecuteQuery("SELECT DATE_FROM,DATE_TO FROM DISCOUNT2 "
                    + "ORDER BY idDISCOUNT2 DESC LIMIT 1");
            rs.next();
            dateFrom = rs.getDate("DATE_FROM");
            dateTo = rs.getDate("DATE_TO");
            if (currentDate.compareTo(dateTo) > 0) {
                con.Execute("DELETE FROM DISCOUNT_APPLICATION WHERE DISCOUNT_NUMBER = 2");
            }

            //3-ci endirim-in DataBase - ni yoxluyuruq eger endirimin vaxtı keçibsə
            //bu endirimin tətbiq olunduğu mallar Endirimli malların
            //sırasında çıxarılır.
            rs = con.ExecuteQuery("SELECT DATE_FROM,DATE_TO FROM DISCOUNT3 "
                    + "ORDER BY idDISCOUNT3 DESC LIMIT 1");
            rs.next();
            dateFrom = rs.getDate("DATE_FROM");
            dateTo = rs.getDate("DATE_TO");
            if (currentDate.compareTo(dateTo) > 0) {
                con.Execute("DELETE FROM DISCOUNT_APPLICATION WHERE DISCOUNT_NUMBER = 3");
            }

        } catch (Exception ex) {
            JOptionPane.showMessageDialog(null,
                    "Xəta baş verdi! \nXəta mesajı = " + ex.getMessage(),
                    "XƏTA", JOptionPane.ERROR_MESSAGE);
        }
    }

    private void formİnternalFrameActivated(javax.swing.event.InternalFrameEvent evt) {//GEN-FIRST:event_formİnternalFrameActivated


        try {
            updateFrame();
            checkDiscounts();
            TableColumn columnModel = jTable2.getColumnModel().getColumn(6);
            DefaultTableCellRenderer renderer =
                    new DefaultTableCellRenderer();
            renderer.setBackground(Color.yellow);
            columnModel.setCellRenderer(renderer);

            columnModel = jTable2.getColumnModel().getColumn(0);
            columnModel.setPreferredWidth(12);

            columnModel = jTable2.getColumnModel().getColumn(3);
            columnModel.setPreferredWidth(35);

        } catch (SQLException ex) {
            JOptionPane.showMessageDialog(null,
                    "Xəta baş verdi! \nXəta mesajı = " + ex.getMessage(),
                    "XƏTA", JOptionPane.ERROR_MESSAGE);

        }
    }//GEN-LAST:event_formİnternalFrameActivated

    private void jPriceCountButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jPriceCountButtonActionPerformed

        DiscountOperations ds = new DiscountOperations();
        productPrices = ds.getInfo((DefaultTableModel) jTable2.getModel());
        double sumPrice = productPrices[productPrices.length - 1];
        if (sumPrice != -1) {
            jLabelTotalPrice.setText(Double.toString(productPrices[productPrices.length - 1]) + " AZN");
        } else {
            JOptionPane.showMessageDialog(null,
                    "Satılan malların siyahısında xəta var!",
                    "XƏTA", JOptionPane.ERROR_MESSAGE);
        }

    }//GEN-LAST:event_jPriceCountButtonActionPerformed

    private void jSaleCancelButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jSaleCancelButtonActionPerformed

        try {
            updateFrame();
        } catch (SQLException ex) {
            JOptionPane.showMessageDialog(null,
                    "Xəta baş verdi! \nXəta mesajı = " + ex.getMessage(),
                    "XƏTA", JOptionPane.ERROR_MESSAGE);
        }
    }//GEN-LAST:event_jSaleCancelButtonActionPerformed

    private void jSellingButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jSellingButtonActionPerformed

        boolean isSold = true;
        DatabaseConnection con = new DatabaseConnection();
        PreparedStatement ps = null;
        try {
            if (jLabelTotalPrice.getText().length() != 0) {

                TableModel model = jTable2.getModel();
                String configFile = "src\\dynamicdiscountsystem\\resources\\ActiveUser.properties";
                Properties config = new Properties();
                int userId = 0;
                String userName = null;

                config.load(new FileReader(configFile));
                userId = Integer.parseInt(config.getProperty("id"));
                userName = config.getProperty("userName");

                int rowCount = model.getRowCount();
                String command;
                for (int i = 0; i < rowCount; i++) {
                    command = "INSERT INTO SOLD_PRODUCTS (idPRODUCT_INPUT,PRODUCT_NAME,"
                            + "PRODUCT_MODEL,PRODUCT_AMOUNT,PRODUCT_SALE_COST,idUSER_INFO,USER_NAME,PRODUCT_SOLD_DATE) "
                            + "VALUES(?,?,?,?,?,?,?,?)";
                    java.sql.Timestamp currentDate = new java.sql.Timestamp(new Date().getTime());

                    con.ConnectionOpen();
                    ps = (PreparedStatement) con.conn.prepareStatement(command);
                    ps.setInt(1, (Integer) model.getValueAt(i, 0));
                    ps.setString(2, (String) model.getValueAt(i, 1));
                    ps.setString(3, (String) model.getValueAt(i, 2));
                    ps.setInt(4, (Integer) model.getValueAt(i, 6));
                    ps.setDouble(5, productPrices[i]);
                    ps.setInt(6, userId);
                    ps.setString(7, userName);
                    ps.setTimestamp(8, currentDate);
                    ps.execute();
                    if (ps != null) {
                        ps.close();
                        con.ConnectionClose();
                    }
                    command = "UPDATE PRODUCT_INPUT SET PRODUCT_AMOUNT = "
                            + ((Integer) model.getValueAt(i, 3) - (Integer) model.getValueAt(i, 6))
                            + " WHERE idPRODUCT_INPUT = " + model.getValueAt(i, 0);
                    con.Execute(command);
                }
                JOptionPane.showMessageDialog(this, "Məlumat əlavə olundu.");
            } else {
                JOptionPane.showMessageDialog(null,
                        "Əvvəlcə malların dəyəri hesablanmalıdır!",
                        "XƏTA", JOptionPane.WARNING_MESSAGE);
            }
            updateFrame();
        } catch (Exception ex) {
            JOptionPane.showMessageDialog(null,
                    "Xəta baş verdi! \nXəta mesajı = " + ex.getMessage(),
                    "XƏTA", JOptionPane.ERROR_MESSAGE);

        } finally {
            try {
                ps.close();
            } catch (SQLException ex) {
                JOptionPane.showMessageDialog(null,
                        "Xəta baş verdi! \nXəta mesajı = " + ex.getMessage(),
                        "XƏTA", JOptionPane.ERROR_MESSAGE);
            }
            con.ConnectionClose();
        }

    }//GEN-LAST:event_jSellingButtonActionPerformed
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton jButtonLeft;
    private javax.swing.JButton jButtonRight;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabelTotalPrice;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JPanel jPanel2;
    private javax.swing.JPanel jPanel4;
    private javax.swing.JPanel jPanel5;
    private javax.swing.JButton jPriceCountButton;
    private javax.swing.JButton jSaleCancelButton;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JScrollPane jScrollPane2;
    private javax.swing.JButton jSellingButton;
    private javax.swing.JTable jTable1;
    private javax.swing.JTable jTable2;
    // End of variables declaration//GEN-END:variables
}
